<?php
namespace common\modules\social\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use common\modules\social\models\query\MessageQuery;

/**
 * This is the model class for table "{{%message}}".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $title
 * @property string $content
 * @property integer $type
 * @property integer $to_user_id
 * @property integer $from_user_id
 * @property integer $is_read
 * @property integer $sort
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property Message $parent
 * @property Message[] $messages
 */
class Message extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%message}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className()
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'to_user_id',
                    'from_user_id'
                ],
                'required'
            ],
            [
                [
                    'parent_id',
                    'type',
                    'to_user_id',
                    'from_user_id',
                    'is_read',
                    'sort',
                    'status'
                ],
                'integer'
            ],
            [
                [
                    'content'
                ],
                'string'
            ],
            [
                [
                    'title'
                ],
                'string',
                'max' => 1024
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', 'ID'),
            'parent_id' => Yii::t('common', 'Parent ID'),
            'title' => Yii::t('common', 'Title'),
            'content' => Yii::t('common', 'Content'),
            'type' => Yii::t('common', 'Type'),
            'to_user_id' => Yii::t('common', 'To User ID'),
            'from_user_id' => Yii::t('common', 'From User ID'),
            'is_read' => Yii::t('common', 'Is Read'),
            'sort' => Yii::t('common', 'Sort'),
            'status' => Yii::t('common', 'Status'),
            'created_at' => Yii::t('common', 'Created At'),
            'updated_at' => Yii::t('common', 'Updated At')
        ];
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getParent()
    {
        return $this->hasOne(Message::className(), [
            'id' => 'parent_id'
        ]);
    }

    /**
     *
     * @return \yii\db\ActiveQuery
     */
    public function getMessages()
    {
        return $this->hasMany(Message::className(), [
            'parent_id' => 'id'
        ]);
    }

    /**
     * @inheritdoc
     *
     * @return MessageQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new MessageQuery(get_called_class());
    }
}
